package com.tim.app.ui.adapter.story;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.application.library.util.TimeUtil;
import com.application.library.widget.RatioImageView;
import com.application.library.widget.recycle.BaseRecyclerAdapter;
import com.application.library.widget.recycle.FlexibleDividerDecoration;
import com.application.library.widget.roundimg.RoundedImageView;
import com.tim.app.R;
import com.tim.app.server.entry.Article;
import com.tim.app.ui.activity.some_homepage.SomeoneHomepageActivity;
import com.tim.app.util.BitmapLoader;

import java.util.List;


/**
 * 文章
 */
public class ArticleAdapter extends BaseRecyclerAdapter<BaseRecyclerAdapter.BaseRecyclerViewHolder, Article>implements FlexibleDividerDecoration.VisibilityProvider {

    private Context mContext;


    public ArticleAdapter(Context mContext, List<Article> mDataList) {
        super(mDataList);
        this.mContext = mContext;
    }

    @Override
    public BaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        BaseRecyclerViewHolder holder = null;
        holder = new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.cell_article, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(BaseRecyclerViewHolder mHolder, int position, final Article data) {
        if (data == null) {
            return;
        }
        ViewHolder holder = (ViewHolder) mHolder;

        if (!TextUtils.isEmpty(data.getTitle())) {
            holder.tvArticleTitle.setText(data.getTitle());
        }
        if (!TextUtils.isEmpty(data.getContent())) {
            holder.tvArticleContent.setText(data.getContent());
        }
        if (null != data.getUserMsg()) {
            if (!TextUtils.isEmpty(data.getUserMsg().getName())) {
                holder.tvUserName.setText(data.getUserMsg().getName());
            }
            BitmapLoader.ins().loadImage(data.getUserMsg().getImage(), holder.rivAvatar);
            holder.tvUserName.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    SomeoneHomepageActivity.start(mContext, data.getUserMsg(), false);
                }
            });
            holder.rivAvatar.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    SomeoneHomepageActivity.start(mContext, data.getUserMsg(), false);
                }
            });
        } else {
            BitmapLoader.ins().loadImage("", holder.rivAvatar);
            holder.tvUserName.setText("");
            holder.tvUserName.setOnClickListener(null);
            holder.rivAvatar.setOnClickListener(null);
        }

        if (!TextUtils.isEmpty(data.getImage())) {
            holder.rivArticleCover.setVisibility(View.VISIBLE);
            BitmapLoader.ins().loadImage(data.getImage(), holder.rivArticleCover);
        } else {
            holder.rivArticleCover.setVisibility(View.GONE);
        }
        holder.tvArticlePriseNumber.setText(mContext.getString(R.string.selectPriseNumber, String.valueOf(data.getAgreeCount())));
        holder.tvArticleCommentNumber.setText(mContext.getString(R.string.selectCommentNumber, String.valueOf(data.getCommentCount())));
        holder.tvPublishTime.setText(TimeUtil.formatTimeRuleOne(mContext, data.getCreateTime()) + mContext.getString(R.string.pointPrompt));
    }

    @Override
    public boolean shouldHideDivider(int position, RecyclerView parent) {
        if (position == 0) {
            return true;
        }
        return false;
    }

    public class ViewHolder extends BaseRecyclerViewHolder {

        RoundedImageView rivAvatar;
        TextView tvUserName;
        RatioImageView rivArticleCover;
        TextView tvArticleTitle, tvArticleContent, tvArticlePriseNumber, tvArticleCommentNumber, tvPublishTime;

        public ViewHolder(View itemView) {
            super(itemView);
            rivAvatar = (RoundedImageView) itemView.findViewById(R.id.rivAvatar);
            tvUserName = (TextView) itemView.findViewById(R.id.tvUserName);
            rivArticleCover = (RatioImageView) itemView.findViewById(R.id.rivArticleCover);
            tvArticleTitle = (TextView) itemView.findViewById(R.id.tvArticleTitle);
            tvArticleContent = (TextView) itemView.findViewById(R.id.tvArticleContent);
            tvArticlePriseNumber = (TextView) itemView.findViewById(R.id.tvArticlePriseNumber);
            tvArticleCommentNumber = (TextView) itemView.findViewById(R.id.tvArticleCommentNumber);
            tvPublishTime = (TextView) itemView.findViewById(R.id.tvPublishTime);
        }

    }

}
